<template>
  <div class="dataTable">
    <div class="header">
      <div class="flex">
        <div class="node_icon" />
        <div>节点列表1</div>
      </div>
    </div>
    <el-table
      v-loading="loading"
      :data="dataList"
    >
      <el-table-column
        prop="nodeId"
        label="节点id"
        width="660"
        show-overflow-tooltip
      >
        <div
          slot-scope="{row}"
          v-clipboard:copy="row.nodeId ? row.nodeId : ''"
          v-clipboard:error="onError"
          v-clipboard:success="onCopy"
          class="textOverflow"
        >
          {{ row.nodeId }}
        </div>
      </el-table-column>
      <el-table-column
        type="index"
        prop="size"
        label="节点类型"
        width="150"
      >
        <div slot-scope="{row}">{{ formatType(row.nodeId) }}</div>
      </el-table-column>
      <el-table-column
        prop="blockNumber"
        label="区块高度"
        width="150"
      />
      <el-table-column
        prop="pbftView"
        label="pbftView"
        width="150"
      >
        <div slot-scope="{row}">{{ row.pbftView || '--' }}</div>
      </el-table-column>
      <el-table-column
        prop="nodeActive"
        label="状态"
        width="160"
      >
        <div slot-scope="{row}">
          <div :class="row.nodeActive === 1 ? 'sucess_point' : 'fail_point'" />{{ statusFormat[row.nodeActive] }}
        </div>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { node_list, getConsensusNodeId } from '@/api/node'
export default {
  name: 'DataTable',
  data() {
    return {
      loading: false,
      nodeTypeList: {
        sealer: '共识',
        observer: '观察',
        remove: '游离'
      },
      statusFormat: {
        1: '运行',
        2: '异常'
      },
      dataList: [],
      status_list: []
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    async loadData() {
      this.loading = true
      try {
        const { code, data } = await node_list()
        if (!code) {
          this.dataList = data
        }
        const res = await getConsensusNodeId({ groupId: 1, pageNumber: 1, pageSize: 100 })
        this.status_list = res.data
        this.loading = false
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    formatType(key) {
      if (!key) return
      for (let i = 0; i < this.status_list.length; i++) {
        const element = this.status_list[i]
        if (key === element.nodeId) {
          return this.nodeTypeList[element.nodeType]
        }
      }
    },
    onCopy() {
      this.$messageOnce.success('复制成功')
    },
    onError() {
      this.$messageOnce.error('复制失败')
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.dataTable {
	width: 1320px;
	min-height: 500px;
	background: #FFFFFF;
	box-shadow: 0px 6px 25px 0px rgba(0, 0, 0, 0.04);
	border-radius: 10px;
	margin: 0 auto;
	margin-top: 30px;
	padding: 20px;
	.header {
		display: flex;
		justify-content: space-between;
		font-size: 18px;
		font-family: SourceHanSansCN-Medium, SourceHanSansCN;
		font-weight: 500;
		color: #2C2C2C;
		margin-bottom: 30px;
		.node_icon {
			width: 24px;
			height: 24px;
			background: url('../../assets/node/node_icon.png');
			background-size: 100%;
			margin-right: 20px;
		}
		.search_input {
			width: 300px;
		}
		.search_btn {
			margin-left: 80px;
		}
	}
}

.textOverflow {
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap;
}

.sucess_point{
  @include points;
  display: inline-block;
  background: #52C41A;
}

.fail_point{
  @include points;
	display: inline-block;
  background: #999999;
}

.flex {
	display: flex;
	align-items: center;
}

::v-deep .el-input__inner{
	border-radius: 30px;
	border: 1px solid #E4E4E4;
}
</style>
